package ModelLayer;
import java.util.ArrayList;

public class Dvd
{
    private static Dvd instance;    
    private ArrayList<Sample> sampleList;
    
    private long id;
    private String title;
    private String author;
    private String releaseDate;
    
    public Dvd(long id, String title, String author, String releaseDate)
    {
        this.id = id;
        this.title = title;
        this.author = author;
        this.releaseDate = releaseDate;
        System.out.println("Dvd has been created!");
        
        sampleList = new ArrayList<Sample>();
        }
    
    //Prints Dvd and Sample info
    public void getInfo()
    {
        System.out.println("ID: "+id);
        System.out.println("Title: "+title);
        System.out.println("Author: "+author);
        System.out.println("Release date: "+releaseDate);
        
        int index = 0;
        while(index < sampleList.size())
        {
            System.out.println("");
            System.out.println("Samples: ");
            sampleList.get(index).getInfo();
            index++;
        }
    }
    
    public void getInfoOnlyDvd()
    {
        System.out.println("ID: "+id);
        System.out.println("Title: "+title);
        System.out.println("Author: "+author);
        System.out.println("Release date: "+releaseDate);
    }
    
    //Changes Dvd attributes
    public void changeInfo(long id, String title, String author, String releaseDate)
    {
        this.id = id;
        this.title = title;
        this.author = author;
        this.releaseDate = releaseDate;
        
        System.out.println("Dvd has been changed!");
    }
    
    public long getId()
    {
        return id;
    }
    
    //Creates and adds an object of type Sample to the sampleList ArrayList
    public void addSample(long serialNo, String acqDate, double acqPrice)
    {
        Sample newSample = new Sample(serialNo, acqDate, acqPrice);
        sampleList.add(newSample);
        System.out.println("Sample has been added!");
    }
    
    //Searches sampleList by searialNo, runs the getInfo() method on sample when found
    public void getSample(long serialNo)
    {
        //this method is to search the container for a person
        //with an id-number equal to the parameter id
        //when person is found, the method is to call print-methods in the TUI layer
        boolean found = false;
        int index = 0;
        while(index < sampleList.size() && found == false)
        {
            String id1 = String.valueOf(serialNo);
            String idIndex = String.valueOf(sampleList.get(index).getSerialNo());
            
            if(idIndex.equals(id1))
            {
                System.out.println("");
                System.out.println("Samples:");
                sampleList.get(index).getInfo();
                found = true;
            }
            else
            {
                index++;
            }
        }
    }
    
    //Search and delete a sample
    public void deleteSample(long serialNo)
    {
        //this method is to search the container for a person
        //with an id-number equal to the parameter id
        //when person is found, the method is to call print-methods in the TUI layer
        boolean found = false;
        int index = 0;
        while(index < sampleList.size() && found == false)
        {
            String id1 = String.valueOf(serialNo);
            String idIndex = String.valueOf(sampleList.get(index).getSerialNo());
            
            if(idIndex.equals(id1))
            {
                sampleList.remove(index);
                System.out.println("Sample has been deleted!");
                found = true;
            }
            else
            {
                index++;
            }
        }
    }
    
    //Search and update a sample
    public void updateSample(long serialNo, String acqDate, double acqPrice)
    {
        int index = 0;
        boolean found = false;

        while(index < sampleList.size() && found == false)
        {
            String id1 = String.valueOf(serialNo);
            String idIndex = String.valueOf(sampleList.get(index).getSerialNo());
                        
            if(idIndex.equals(id1))
            {
                sampleList.get(index).changeInfo(serialNo, acqDate, acqPrice);
                found = true;
            }
            else
            {
                index++;
            }
        }
    }
}